package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletdFlageEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoEnum;
import com.jingdianjichi.subject.domain.convert.SubjectMultipleConverter;
import com.jingdianjichi.subject.domain.convert.SubjectRadioConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * SubjectRadioHandler 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/20 21:06
 * @description 单选策略
 */
@Component
public class SubjectRadioHandler implements SubjectTypeHandler{
    @Resource
    SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoEnum getSubjectType() {
        return SubjectInfoEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectRadio> subjectRadioList = new ArrayList<>();
        List<SubjectOptionBO> subjectOptionList = subjectInfoBO.getSubjectOptionList();
        subjectOptionList.forEach(option -> {
            SubjectRadio subjectRadio = SubjectRadioConverter.INSTANCE.convertBoToEntity(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletdFlageEnum.NOT_DELETED.getCode());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.insertBatch(subjectRadioList);


    }

    @Override
    public SubjectAnswerBO query(SubjectInfoBO subjectInfoBO) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectInfoBO.getId());
        List<SubjectRadio> subjectRadioList = subjectRadioService.query(subjectRadio);
        List<SubjectOptionBO> subjectOptionBOList = SubjectRadioConverter.INSTANCE.convertEntityToBoList(subjectRadioList);
        SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
        subjectAnswerBO.setSubjectOptionList(subjectOptionBOList);
        return subjectAnswerBO;
    }
}
